package cn.hxzy.office.controller;

import cn.hxzy.office.entity.*;
import cn.hxzy.office.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author little black
 * 这个是管理者页面
 */
@Controller
public class PublicController {
    @GetMapping("/")
//    页面框
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("index");
        return modelAndView;
    }

    @GetMapping("/main")
//    主页面
    public ModelAndView main() {
        ModelAndView modelAndView = new ModelAndView("main");
        return modelAndView;
    }

    @GetMapping("/userIndex")
//    主页面
    public ModelAndView userIndex() {
        return new ModelAndView("userIndex");
    }


    /**
     * 人事管理页面
     */
    @Autowired
    private UserService userService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private NationalService nationalService;
    @Autowired
    private EducationService educationService;
    @Autowired
    private DeptScheduleService deptScheduleService;
    @Autowired
    private NewsService newsService;
    @Autowired
    private AttendanceService attendanceService;
    @Autowired
    private ProjectService projectService;

    @GetMapping("/createUser")
//  新增员工
    public ModelAndView createUser() {
        ModelAndView modelAndView = new ModelAndView("createUser");
        List<Dept> list = deptService.queryAll();
        modelAndView.addObject("depts", list);
        List<National> nationals = nationalService.queryAll();
        modelAndView.addObject("nationals", nationals);
        List<Education> educations = educationService.queryAll();
        modelAndView.addObject("educations", educations);
        return modelAndView;
    }

    @ModelAttribute
    //确定个初始值
    public void test(Map<String, Object> map) {
        User user = new User();
        Project project = new Project();
        project.setId(1);
        user.setRoleId(1);
        user.setProject(project);
        map.put("user", user);
    }

    @PostMapping("/saveUser")
//  新增员工->重定向到页面
    public String saveUser(User user) {
        user.setNumber("11" + System.currentTimeMillis());
        userService.insert(user);
        return "redirect:/searchUser";
    }

    @GetMapping("/searchUser")
//  员工信息查询
    public ModelAndView searchUser() {
        ModelAndView modelAndView = new ModelAndView("searchUser");
        List<User> list = userService.queryAll();
        modelAndView.addObject("users", list);
        return modelAndView;
    }

    @GetMapping("/deleteUser/{id}")
//  删除员工
    public String deleteUser(@PathVariable("id") Integer id) {
        userService.deleteById(id);
        return "redirect:/searchUser";
    }

    @GetMapping("/updateUser/{id}")
//  编辑员工
    public ModelAndView updateUser(@PathVariable("id") Integer id) {
        ModelAndView modelAndView = new ModelAndView("updateUser");
        modelAndView.addObject("user", userService.queryById(id));
        List<Dept> list = deptService.queryAll();
        modelAndView.addObject("depts", list);
        List<National> nationals = nationalService.queryAll();
        modelAndView.addObject("nationals", nationals);
        List<Education> educations = educationService.queryAll();
        modelAndView.addObject("educations", educations);
        return modelAndView;
    }

    //    员工修改->重定向到页面
    @PostMapping("/editUser/{id}")
    public String editUser(User user) {
        user.setNumber(null);
        user.setRoleId(null);
        userService.update(user);
        return "redirect:/searchUser";
    }

    @GetMapping("/createDept")
//  新增部门
    public ModelAndView createDept() {
        ModelAndView modelAndView = new ModelAndView("createDept");
        return modelAndView;
    }

    @PostMapping("/saveDept")
//    新增部门->重定向
    public String saveDept(Dept dept) {
        dept.setNumber("12" + System.currentTimeMillis());
        deptService.insert(dept);
        return "redirect:/searchDept";
    }

    @ModelAttribute
    //确定个初始值
    public void test2(Map<String, Object> map) {
        Dept dept = new Dept();
        map.put("dept", dept);
    }

    @GetMapping("/searchDept")
//  部门信息查询
    public ModelAndView searchDept() {
        ModelAndView modelAndView = new ModelAndView("searchDept");
        List<Dept> list = deptService.queryAll();
        modelAndView.addObject("depts", list);
        return modelAndView;
    }

    @GetMapping("/updateDept/{id}")
//  编辑部门
    public ModelAndView updateDept(@PathVariable("id") Integer id) {
        ModelAndView modelAndView = new ModelAndView("updateDept");
        modelAndView.addObject(deptService.queryById(id));
        return modelAndView;
    }

    @PostMapping("/editDept/{id}")
//  部门修改->重定向到页面
    public String editDept(Dept dept) {
        deptService.update(dept);
        return "redirect:/searchDept";
    }

    @RequestMapping("/deleteDept/{id}")
//  删除部门
    public String deleteDept(@PathVariable("id") Integer id) {
        deptService.deleteById(id);
        return "redirect:/searchDept";
    }


    /**
     * 日程管理页面
     */
    @GetMapping("/createDeptSchedule")
//  新增部门日程
    public ModelAndView createDeptSchedule() {
        ModelAndView modelAndView = new ModelAndView("createDeptSchedule");
        List<Dept> list = deptService.queryAll();
        modelAndView.addObject("depts", list);
        return modelAndView;
    }

    @PostMapping("/saveDeptSchedule")
//    新增部门->重定向
    public String saveDeptSchedule(DeptSchedule deptSchedule) {
        System.out.println(deptSchedule);
        deptScheduleService.insert(deptSchedule);
        return "redirect:/searchDeptSchedule";
    }

    @GetMapping("/searchDeptSchedule")
//  部门日程管理
    public ModelAndView searchDeptSchedule() {
        ModelAndView modelAndView = new ModelAndView("searchDeptSchedule");
        List<DeptSchedule> list = deptScheduleService.queryAll();
        modelAndView.addObject("deptSchedules", list);
        return modelAndView;
    }

    @RequestMapping("/deleteDeptSchedule/{id}")
//  删除日程
    public String deleteDeptSchedule(@PathVariable("id") Integer id) {
        deptScheduleService.deleteById(id);
        return "redirect:/searchDeptSchedule";
    }

    @GetMapping("/updateDeptSchedule/{id}")
//  编辑日程
    public ModelAndView updateDeptSchedule(@PathVariable("id") Integer id) {
        ModelAndView modelAndView = new ModelAndView("updateDeptSchedule");
        List<Dept> list = deptService.queryAll();
        modelAndView.addObject("depts", list);
        modelAndView.addObject(deptScheduleService.queryById(id));
        return modelAndView;
    }

    @PostMapping("/editDeptSchedule/{id}")
//  日程修改->重定向到页面
    public String editDeptSchedule(DeptSchedule deptSchedule) {
        deptScheduleService.update(deptSchedule);
        return "redirect:/searchDeptSchedule";
    }

    /**
     * 文档管理页面
     */
    @GetMapping("/searchUserDoc")
//  员工档案查询
    public ModelAndView searchUserDoc() {
        ModelAndView modelAndView = new ModelAndView("searchUserDoc");
        List<User> users = userService.findAll();
        modelAndView.addObject("users", users);
        return modelAndView;
    }


    /**
     * 公告管理页面
     */
    @GetMapping("/createNews")
//  发布公告
    public ModelAndView createNews() {
        ModelAndView modelAndView = new ModelAndView("createNews");
        return modelAndView;
    }

    @PostMapping("/saveNews")
//    新增公告->重定向
    public String saveNews(News news) {
        newsService.insert(news);
        return "redirect:/searchNews";
    }

    @ModelAttribute
    //确定个初始值
    public void test3(Map<String, Object> map) {
        News news = new News();
        User user = new User();
        Attendance attendance = new Attendance();
        attendance.setRecordDate(new Date());
        user.setId(11);
        news.setUser(user);
        news.setCreateTime(new Date());
        map.put("news", news);
    }

    @GetMapping("/searchNews")
//  公告查询
    public ModelAndView searchNews() {
        ModelAndView modelAndView = new ModelAndView("searchNews");
        List<News> list = newsService.queryAll();
        modelAndView.addObject("news", list);
        return modelAndView;
    }

    @RequestMapping("/deleteNews/{id}")
//  删除日程
    public String deleteNews(@PathVariable("id") Integer id) {
        newsService.deleteById(id);
        return "redirect:/searchNews";
    }

    @GetMapping("/updateNews/{id}")
//  编辑日程
    public ModelAndView updateNews(@PathVariable("id") Integer id) {
        ModelAndView modelAndView = new ModelAndView("updateNews");
        List<News> news = newsService.queryAll();
        modelAndView.addObject("news", news);
        modelAndView.addObject(newsService.queryById(id));
        return modelAndView;
    }

    @PostMapping("/editNews/{id}")
//  日程修改->重定向到页面
    public String editNews(News news) {
        newsService.update(news);
        return "redirect:/searchNews";
    }


    /**
     * 考勤管理页面
     */
    @GetMapping("/createAttendance")
//  录入考勤数据
    public ModelAndView createAttendance() {
        ModelAndView modelAndView = new ModelAndView("createAttendance");
        List<User> users = userService.queryAll();
        modelAndView.addObject("users", users);
        return modelAndView;
    }

    @PostMapping("/saveAttendance")
//    录入考勤->重定向岩页面
    public String saveAttendance(Attendance attendance) {
        System.out.println("******\n" + attendance + "\n************");
        attendanceService.insert(attendance);
        return "redirect:/searchAttendance";
    }

    @GetMapping("/searchAttendance")
//  考勤数据查询
    public ModelAndView searchAttendance() {
        ModelAndView modelAndView = new ModelAndView("searchAttendance");
        List<Attendance> attendance = attendanceService.queryAll();
        modelAndView.addObject("attendances", attendance);
        return modelAndView;
    }


    /**
     * 薪酬管理页面
     */
    @GetMapping("/searchUserWages")
//  薪酬查询
    public ModelAndView searchUserWages() {
        ModelAndView modelAndView = new ModelAndView("searchUserWages");
        List<User> users = userService.findAllWithWages();
        modelAndView.addObject("users", users);
        return modelAndView;
    }

    @PostMapping("/updateUserWages/{id}")
//  编辑薪资->重定向页面
    public String updateUserWages(User user) {
        System.out.println("******\n" + user + "\n************");
        userService.update(user);
        return "redirect:/searchUserWages";
    }

    /**
     * 项目管理页面
     */
    @GetMapping("/createProject")
//  新增项目
    public ModelAndView createProject() {
        ModelAndView modelAndView = new ModelAndView("createProject");
        return modelAndView;
    }

    @PostMapping("/saveProject")
//    重定向
    public String saveProject(Project project) {
        projectService.insert(project);
        return "redirect:/searchProject";
    }

    @GetMapping("/searchProject")
//  项目信息查询
    public ModelAndView searchProject() {
        ModelAndView modelAndView = new ModelAndView("searchProject");
        List<Project> projects = projectService.queryAll();
        System.out.println(projects);
        modelAndView.addObject("projects", projects);
        return modelAndView;
    }

    @RequestMapping("/deleteProject/{id}")
//    项目删除
    public String deleteProject(@PathVariable("id") Integer id) {
        projectService.deleteById(id);
        return "redirect:/searchProject";
    }

    @GetMapping("/updateProject/{id}")
//    修改项目
    public ModelAndView updateProject(@PathVariable("id") Integer id) {
        ModelAndView modelAndView = new ModelAndView("updateProject");
        modelAndView.addObject(projectService.queryById(id));
        return modelAndView;
    }

    @PostMapping("/editProject/{id}")
//    重定向
    public String editProject(Project project) {
        projectService.update(project);
        return "redirect:/searchProject";
    }
}
